﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Enhancements.Rest.Filters
{
    /// <summary>
    /// <para>Attribute that allows to set restriction on HTTP method (GET or POST, DELETE, PUT) to REST service method.</para>
    /// <para>This attribute is an optional. By default both GET and POST requests are allowed to be processed. 
    /// To use DELETE and PUT make sure ASP.NET web.config and IIS allow these HTTP methods.</para>
    /// </summary>
    [Serializable()]
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
    public class WebMethodFilterAttribute : WebRestFilterAttribute
    {
        /// <summary>
        /// Gets or sets the HTTP Method as HttpMethodRequirement that is allowed for this method.
        /// </summary>
        public HttpMethodRequirement HttpMethod { get; set; }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public WebMethodFilterAttribute()
            : this(HttpMethodRequirement.GetOrPost)
        {
        }

        /// <summary>
        /// Constructor that accepts HTTP method requirement.
        /// </summary>
        /// <param name="httpMethod">HTTP method requirement.</param>
        public WebMethodFilterAttribute(HttpMethodRequirement httpMethod)
            : base()
        {
            this.HttpMethod = httpMethod;
        }

        public override System.Type FilterType
        {
            get
            {
                return typeof(WebMethodFilter);
            }
            set
            {
                //does nothing - should not reset value. //just for interface
            }
        }

        public override IWebRestFilter CreateFilter()
        {
            return new WebMethodFilter()
            {
                HttpMethod = this.HttpMethod
            };
        }

        public override string ToString()
        {
            return string.Format("{0}: {1}", base.ToString(), this.HttpMethod);
        }
    }
}
